package com.ybbase.framework.config.activiti.custom.groups;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ybbase.framework.config.activiti.custom.util.ActivitiUserUtil;
import com.ybbase.framework.model.po.system.Organization;
import com.ybbase.framework.service.system.OrganizationService;
import com.ybbase.framework.service.system.UserService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.GroupQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
 * @author cg
 * @date 2019-08-22 15:03
 * @DEC
 */
@Component
public class CustomGroupEntityManager extends GroupEntityManager{
    @Lazy
    @Autowired
    private OrganizationService organizationService;

    @Lazy
    @Autowired
    private UserService userService;


    @Override
    public List<Group> findGroupsByUser(String userId) {
        if (userId == null){
            return null;
        }
        try {
            List<String> listStr = new ArrayList<>();
            List<Organization> list = organizationService.getOrgListByUserId(Integer.valueOf(userId));
            if(list!=null && list.size()>0){
                list.forEach(Organization ->{listStr.add(Organization.getId()+"");});
            }
            List<Group> gs = ActivitiUserUtil.toActivitiGroups(listStr);
            return gs;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public long findGroupCountByQueryCriteria(GroupQueryImpl query) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public List<Group> findGroupsByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public long findGroupCountByNativeQuery(Map<String, Object> parameterMap) {
        throw new RuntimeException("not implement method.");
    }



}
